package com.main.dao;

import java.util.Collections;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.generico.base.BaseDaoImpl;
import com.generico.exception.AsiWebException;
import com.main.cliente.dto.Pedido;
import com.main.java.Instalador;
import com.main.java.Inventario;
import com.main.java.Requisicion;
import com.main.java.User;

@Transactional
@Repository
public class InstaladorDaoImpl extends BaseDaoImpl implements InstaladorDao{
	
	@Autowired
	private SessionFactory sessionFactory;

	private Session openSession() {
		return sessionFactory.getCurrentSession();
	}
	
	public List<Object[]> obtenerInstalador() {
		Criteria criteria = openSession().getSessionFactory().getCurrentSession().createCriteria(Instalador.class);
		criteria.createAlias("usuarioInstaladorId","usuarioInstaladorId");
		criteria.createAlias("empleadoCreadorGrupoId","empleadoCreadorGrupoId");
		criteria.setProjection(Projections.distinct(
				Projections.projectionList().
				add(Projections.property("instaladorId")).
				add(Projections.property("instaladorNombreGrupo")).
				add(Projections.property("instaladorFechaCreacion")).
				add(Projections.property("instaladorEstado")).
				add(Projections.property("usuarioInstaladorId.login")).
				add(Projections.property("empleadoCreadorGrupoId.login"))));
		criteria.add(Restrictions.eq("instaladorEstado", "1"));
		criteria.addOrder(Order.asc("instaladorId"));
		try {
			return (List<Object[]>) findByCriteria(criteria);
		} catch (AsiWebException e) {
			logger.error(e, e);
			return Collections.emptyList();
		}
	}
	
	public void guardarOActualizarInstalador(Instalador instalador) throws AsiWebException{
		save(instalador);
	}
	
	public Object[] obtenerInstaladorPorId(Long instaladorId) throws AsiWebException {
		Object[] instalador;
		instalador = (Object[]) findById(Instalador.class, instaladorId);
			return instalador;
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> obtenerUsuariosInstaladoresPorId(Long id) throws AsiWebException {
		Criteria criterias = openSession().getSessionFactory().getCurrentSession().createCriteria(User.class);
		criterias.createAlias("role", "role");
		
		criterias.setProjection(Projections.distinct(
				Projections.projectionList().
					add(Projections.property("id")).
					add(Projections.property("login")).
					add(Projections.property("usuarioActivo"))
				));
		criterias.add(Restrictions.eq("id", id));
		criterias.addOrder(Order.asc("id"));

		try {
			return (List<Object[]>) findByCriteria(criterias);
		} catch (AsiWebException e) {
			logger.error(e, e);
			return Collections.emptyList();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> obtenerUsuariosInstaladores() throws AsiWebException {
		Criteria criterias = openSession().getSessionFactory().getCurrentSession().createCriteria(User.class);
		criterias.createAlias("role", "role");
		
		criterias.setProjection(Projections.distinct(
				Projections.projectionList().
					add(Projections.property("id")).
					add(Projections.property("login")).
					add(Projections.property("usuarioActivo"))
				));
		criterias.add(Restrictions.and()
				.add(Restrictions.eq("usuarioActivo", "1"))
				.add(Restrictions.eq("role.roleId", 7L)));
		criterias.addOrder(Order.asc("id"));

		return (List<Object[]>) findByCriteria(criterias);
	}
	
	public List<Object[]> obtenerListaInstalador() {
		Criteria criteria = openSession().getSessionFactory().getCurrentSession().createCriteria(Instalador.class);
		criteria.createAlias("usuarioInstaladorId","usuarioInstaladorId");
		criteria.setProjection(Projections.distinct(
				Projections.projectionList().
				add(Projections.property("usuarioInstaladorId.id")).
				add(Projections.property("usuarioInstaladorId.login")).
				add(Projections.property("usuarioInstaladorId.usuarioActivo"))));
		
		criteria.add(Restrictions.eq("instaladorEstado", "1"));
		criteria.addOrder(Order.asc("usuarioInstaladorId.id"));
		try {
			return (List<Object[]>) findByCriteria(criteria);
		} catch (AsiWebException e) {
			logger.error(e, e);
			return Collections.emptyList();
		}
	}
	
	public Instalador encInstaladorPorId(Long instaladorId) throws AsiWebException {
		Instalador instalador = new Instalador();
		instalador = (Instalador) findById(Instalador.class, instaladorId);
		return instalador;	
	} 
	
	public List<Object[]> obtenerInstaladorPorGrupoId(Long grupoId) {
		Criteria criteria = openSession().getSessionFactory().getCurrentSession().createCriteria(Instalador.class);
		criteria.createAlias("usuarioInstaladorId","usuarioInstaladorId");
		criteria.createAlias("empleadoCreadorGrupoId","empleadoCreadorGrupoId");
		criteria.setProjection(Projections.distinct(
				Projections.projectionList().
				add(Projections.property("instaladorId")).
				add(Projections.property("instaladorNombreGrupo")).
				add(Projections.property("instaladorFechaCreacion")).
				add(Projections.property("instaladorEstado")).
				add(Projections.property("usuarioInstaladorId.id")).
				add(Projections.property("empleadoCreadorGrupoId.id"))));
		criteria.add(Restrictions.eq("instaladorEstado", "1"));
		criteria.add(Restrictions.eq("instaladorNombreGrupoId", grupoId));
		criteria.addOrder(Order.asc("instaladorId"));
		try {
			return (List<Object[]>) findByCriteria(criteria);
		} catch (AsiWebException e) {
			logger.error(e, e);
			return Collections.emptyList();
		}
	}
	
}